﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ToDoList.Entity.Models;

namespace ToDoList.Logic
{

    public class UserFunction
    {
        private static TodoContext _ctx = new TodoContext();
        //用户登录
        /// <summary>
        /// 登录需要ID和密码，登录成功时返回用户类型的返回值
        /// </summary>
        /// <param name="id"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public Users Login(string userName, string password)
        {
            var result = _ctx.Users.SingleOrDefault(prop => prop.UserName==userName   && prop.Password.Equals(password));
        
            if (result != null)
                return result;
            else
                throw new Exception("The password or ID was wrong!");
        }
        //注册用户
        /// <summary>
        /// 用户需要输入昵称，密码，用户名，注册成功返回一个布尔类型true
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newNickname"></param>
        /// <param name="newPassword"></param>
        /// <param name="newName"></param>
        /// <returns></returns>
        public void  Enroll(string newNickname, string newPassword, string newName)
        {
            if(_ctx.Users.SingleOrDefault (prop=>prop.UserName == newName)== null)
            {
                    Users user = new Users()
                    {
                        Nickname = newNickname,
                        Password = newPassword,
                        UserName = newName,
                    };
                 _ctx.Users.Add(user);
                 _ctx.SaveChanges();
            }
            else
            {
                throw new Exception("用户名已存在");
            }


                 
                 
      
        }
    }

}
